package com.globagames.strategicgames.command
{
	import com.adobe.cairngorm.commands.Command;
	import com.adobe.cairngorm.control.CairngormEvent;
	import com.globagames.isometric.core.business.FileAccessDelegate;
	import com.globagames.isometric.core.util.Observe;
	import com.globagames.strategicgames.event.LoadFactionArmyEvent;
	import com.globagames.strategicgames.model.IArmyModule;
	import com.globagames.strategicgames.model.StrategicGameModelLocator;
	import com.globagames.strategicgames.util.AlarmEvent;
	
	import flash.system.ApplicationDomain;
	
	import mx.events.ModuleEvent;
	import mx.modules.ModuleLoader;
	import mx.rpc.IResponder;

	public class LoadFactionArmyCommand implements Command, IResponder
	{
		private var delegate : FileAccessDelegate;
		private var e:LoadFactionArmyEvent;
		private var currentTask:String;
		private var moduleLoader:ModuleLoader;
		private var model:StrategicGameModelLocator = StrategicGameModelLocator.getInstance();
		
		private var observer:Observe;
		public function LoadFactionArmyCommand()
		{
			
		}

		public function execute(event:CairngormEvent):void
		{
			e = event as LoadFactionArmyEvent;
			model.gameModuleManager.setModule(e.factionCode);
			
			
			model.gameModuleManager.addEventListener(AlarmEvent.ALARM,loaded);
			trace("BEFORE");
			model.gameModuleManager.loadModule();
			trace("AFTER");
			
		}
		public function loaded( evt:AlarmEvent):void
		{
			e.armyModule = evt.armyModule;
			model.armiesModules[e.factionCode] = e.armyModule;	
			model.gameModuleManager.removeEventListener(AlarmEvent.ALARM,loaded);			
			trace("loaded !!!!!!!!!!!!!!!!!! "+ model.gameModuleManager.moduleLoaded);
			e.factionArmyLoaded = true;
		}
		public function execute2(event:CairngormEvent):void
		{		
			trace("LoadFactionArmyCommand execute");
			e = event as LoadFactionArmyEvent;
			moduleLoader = model.armiesModules[e.factionCode] as ModuleLoader;
			
			if( moduleLoader ) {
				 e.armyModule = moduleLoader.child as IArmyModule;
				 e.factionArmyLoaded = true;	
			} else {
				moduleLoader = new ModuleLoader();
				moduleLoader.addEventListener(ModuleEvent.READY, moduleReadyHandler);
				moduleLoader.addEventListener(ModuleEvent.ERROR, fault);
				moduleLoader.applicationDomain = ApplicationDomain.currentDomain;
				
				
				
				if( e.factionCode == 'GRIFFINS' ) {
					moduleLoader.url = "modules/armies/GriffinsArmyModule.swf";
				} else if( e.factionCode == 'WOLFENS' ) {
					
					moduleLoader.url = "modules/armies/WolfenArmyModule.swf";
				}
				trace("url set:"+moduleLoader.url);
				if( moduleLoader.loaderInfo ) {
				trace("will trigger READY event ?:"+moduleLoader.loaderInfo.willTrigger(ModuleEvent.READY));
				}
				moduleLoader.unloadModule();
				moduleLoader.loadModule();	
			}
		}
		public function result( event : Object ) : void
		{	
			
			
		}
		private function moduleReadyHandler(event:ModuleEvent):void {
			trace("moduleReadyHandler");
				e.armyModule = moduleLoader.child as IArmyModule;
				e.armyModule.initialize2();
				model.armiesModules[e.factionCode] = moduleLoader;				
				e.factionArmyLoaded = true;																						
			}
		public function fault( event : Object ) : void
		{			
			trace(event.fault.rootCause.message, event.fault.faultCode);
		}
		
	}
}